import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib

# 设置字体为Times New Roman，字号为10pt
matplotlib.rcParams['font.sans-serif'] = ['Times New Roman']
matplotlib.rcParams['font.size'] = 10

# 创建一个三维坐标系
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

# 定义长方体的顶点坐标
vertices = np.array([[0, 0, 0],
                     [8, 0, 0],
                     [8, 1, 0],
                     [0, 1, 0],
                     [0, 0, 2],
                     [8, 0, 2],
                     [8, 1, 2],
                     [0, 1, 2]])

# 绘制长方体的顶点
ax.scatter(vertices[:, 0], vertices[:, 1], vertices[:, 2], color='black')

# 标注音频传感器位置
audio_sensor_points = np.array([[4, 0, 1], [4, 1, 1], [8, 0.5, 1]])
ax.scatter(audio_sensor_points[:, 0], audio_sensor_points[:, 1], audio_sensor_points[:, 2], color='red', s=100)  # 灰色表示音频传感器

# 在每个音频传感器位置添加注释
annotations = ["Audio Sensor", "Audio Sensor", "Audio Sensor"]
offsets = [(0.5, 0.5, 0), (0.5, 0.25, 0), (0.5, 0, 0)]  # 调整注释偏移量以避免遮挡
for point, label, offset in zip(audio_sensor_points, annotations, offsets):
    ax.text(point[0] + offset[0], point[1] + offset[1], point[2] + offset[2], label, color='black', fontsize=8)

# 添加坐标轴的箭头
ax.quiver(0, 0, 0, 3, 0, 0, color='black', arrow_length_ratio=0.1)
ax.quiver(0, 0, 0, 0, 4, 0, color='black', arrow_length_ratio=0.1)
ax.quiver(0, 0, 0, 0, 0, 5, color='black', arrow_length_ratio=0.1)

# 定义长方体的六个面
faces = [[vertices[0], vertices[1], vertices[5], vertices[4]],
         [vertices[7], vertices[6], vertices[2], vertices[3]],
         [vertices[0], vertices[3], vertices[7], vertices[4]],
         [vertices[1], vertices[2], vertices[6], vertices[5]],
         [vertices[4], vertices[5], vertices[6], vertices[7]],
         [vertices[0], vertices[1], vertices[2], vertices[3]]]

# 创建长方体的面并设置为浅灰色
ax.add_collection3d(Poly3DCollection(faces, facecolors='lightgrey', linewidths=1, edgecolors='black', alpha=.5))

# 设置坐标轴范围
ax.set_xlim([0, 10])
ax.set_ylim([0, 3])
ax.set_zlim([0, 5])

# 设置坐标轴标签
ax.set_xlabel('X', fontname='Times New Roman')
ax.set_ylabel('Y', fontname='Times New Roman')
ax.set_zlabel('Z', fontname='Times New Roman')

# 设置背景颜色
ax.set_facecolor('whitesmoke')

# 显示图形
plt.show()
